<template>
  <div id="ReceiptPreflight" v-loading="loading">
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" :title="$t('in.receipPreLight')" @del="del" @exportFile="exportFile"  @createASN="createASN" @handleDownErrorFile="exceptionFileUrl"
      @handleDownTemplate="downloadExcelFile"
      @newCreate="newCreate"></v-tableTitle>
    <v-table :pageSize="pageSize" :page="page" :tableConfig="tableConfig" :tableData="tableData" :total="total"
      @findData="getTableData" @getTableData="getTableData" @searhData="searhData" @selectData="selectData"
      @setdialog="setdialog" @upData="getTableData" ref="table"></v-table>
    <el-dialog :modal="false" :visible.sync="dialogFlag" title="" top="0" v-dialogDrag :close-on-click-modal="false"
      width="900px">
      <router-view></router-view>
    </el-dialog>
  </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import config from "./Config";

export default {
  name: "receiptPreflight",
  mixins: [baseMixin, config],
  data() {
    return {
      loading: false,
      tableConfig: [
        {
          label: this.$t('in.receipt'),
          prop: "receipt",
          input: "select",
          options: [
            { code: 'Y', description: this.$t('code.yes') },
            { code: 'N', description: this.$t('code.no') }
          ],
        },
        {
          label: this.$t('in.receiptpreflightkey'),
          prop: "receiptpreflightkey",
          input: "input"
        },
        {
          label: this.$t('in.importkey'),
          prop: "importkey",
          input: "input"
        },
        {
          label: this.$t('in.excelgroupkey'),
          prop: "excelgroupkey",
          input: "input"
        },
        {
          label: "ASN",
          prop: "receiptkey",
          input: "input"
        },
        {
          label: this.$t('in.receipttype'),
          prop: "receipttype",
          input: "select",
          options: this.getOptions("codelkup/RECEIPTYPE")
        },
        {
          label: this.$t('config.supplier'),
          prop: "vendor",
          input: "input"
        },
        {
          label: this.$t('config.shipper'),
          prop: "storerkey",
          input: "input"
        },
        {
          label: this.$t('config.sku'),
          prop: "sku",
          input: "input"
        },
        {
          label: this.$t('in.generatedate'),
          prop: "generatedate",
          input: "date"
        },
        {
          label: this.$t('in.vendorbatch'),
          prop: "vendorbatch",
          input: "input"
        },
        {
          label: this.$t('in.origin'),
          prop: "origin",
          input: "input"
        },
        {
          label: this.$t('in.legalkey'),
          prop: "legalkey",
          input: "input"
        },
        {
          label: this.$t('in.potype'),
          prop: "potype",
          input: "select",
          options: this.getOptions("codelkup/JDEPOTYPE")
        },
        {
          label: this.$t('in.pono'),
          prop: "pono",
          input: "input"
        },
        {
          label: this.$t('in.arrivalqty'),
          prop: "arrivalqty",
          input: "input"
        },
        {
          label: this.$t('outbound.uom'),
          prop: "uom",
          input: "select",
          options: this.getOptions("codelkup/defaultUom/STD")
        },
        {
          label: this.$t('in.lastdate'),
          prop: "lastdate",
          input: "date"
        },
        {
          label: this.$t('in.specialresult'),
          prop: "specialresult",
          input: "input"
        },
        {
          label: this.$t('in.skuneed'),
          prop: "skuneed",
          input: "input"
        },
        {
          label: this.$t('in.expectedreceiptdate1'),
          prop: "expectedreceiptdate",
          input: "date"
        },
        {
          label: this.$t('in.lottable08'),
          prop: "lottable08",
          input: "input"
        },
        {
          label: this.$t('in.externpokey'),
          prop: "externpokey",
          input: "input"
        },

      ],
      tableData: [],
      pageSize: 10,
      page: 1,
      total: 10,
      dialogFlag: false,
      tableSelectData: [],
      tableSearhData: {},
      // 收货预检信息
      importType: 10317
      // 导入可视化 end
    };
  },
  computed: {
    btnConfig() {
      return [
        { label: this.$t('common.create'), certigier: this.newStatus || !this.btnPermNameList.includes('WM011101'), invalid: "newCreate" },
        { label: this.$t('common.delete'), certigier: this.deleteStatus || !this.btnPermNameList.includes('WM011102'), disabled: true, invalid: "del" },
        { type: "file", certigier: this.poiStatus || !this.btnPermNameList.includes('WM011103'), label: this.$t('common.import'), invalid: "file",importType:10317,importUrl:"receiptPreflight/importReceiptPreflight",downTemplate:"handleDownTemplate"},
        // { label: this.$t('config.importTemDownload'), certigier: this.poiStatus || !this.btnPermNameList.includes('WM011104'), invalid: "downloadExcelFile" },
        { label: this.$t('in.createASN'), certigier: this.newStatus || !this.btnPermNameList.includes('WM011105'), invalid: "createASN" },
        // {label: this.$t('common.export'), certigier: this.poiStatus, disabled: true, invalid: "exportFile"},
      ]
    }
  },
  created() {
  },
  methods: {
    async exportFile(data) {
      try {
        let res = await this.$api.post('/export', data, { responseType: 'blob' })
        if (window.navigator.msSaveBlob) {
          var blobObject = new Blob([res.data]);
          window.navigator.msSaveBlob(blobObject, '表.xls');
        } else {
          var blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8' });
          var filename = '表.xls';
          var downloadElement = document.createElement('a');
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.style.display = 'none';
          downloadElement.href = href;
          downloadElement.download = filename; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
        }
      } catch (e) {
        this.$message.error(e);
      } finally {
        
      }
    },
    exceptionFileUrl(exceptionUrl) {

      console.log("exceptionFileUrl = %o", 1);
      const data = [{
        exceptionFileUrl: exceptionUrl
      }];

      // 异常下载配置
      let urlConf = { responseType: 'blob' };
      let exceptionFileName = "收货预检导入模板异常文件.xls";
      let url = '/comExcelImportResult/downloadExceptionFile';
      let options = { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8' };

      try {
        this.$api.post(url, data, urlConf).then(res => {
          let blobParts = [res.data];
          if (window.navigator.msSaveBlob) {
            const blobObject = new Blob(blobParts);
            window.navigator.msSaveBlob(blobObject, exceptionFileName);
          } else {
            let tagName = 'a';
            const blob = new Blob(blobParts, options);
            const downloadElement = document.createElement(tagName);
            // 创建下载的链接
            const href = window.URL.createObjectURL(blob);
            downloadElement.style.display = 'none';
            downloadElement.href = href;
            // 下载后文件名
            downloadElement.download = exceptionFileName;
            document.body.appendChild(downloadElement);
            // 点击下载
            downloadElement.click();
            // 下载完成移除元素
            document.body.removeChild(downloadElement);
            // 释放掉blob对象
            window.URL.revokeObjectURL(href);
          }
          this.importVisualDialogFlag = false;
          this.initialize();
        });
      } catch (e) {
        this.$message.error(e);
      } finally {
        
      }
    },
    async downloadExcelFile(data) {
      console.log('fdjkgnfdjklng');
      try {
        let xls = "收货预检导入模板.xls";
        let url = "/downloadExcelFile/" + xls;
        let res = await this.$api.post(url, {}, { responseType: 'blob' })
        if (window.navigator.msSaveBlob) {
          var blobObject = new Blob([res.data]);
          window.navigator.msSaveBlob(blobObject, xls);
        } else {
          var blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8' });
          var filename = xls;
          var downloadElement = document.createElement('a');
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.style.display = 'none';
          downloadElement.href = href;
          downloadElement.download = filename; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
        }
      } catch (e) {
        this.$message.error(e);
      } finally {
        if (data.length !== 0) {
          
        }
      }
    },
    //新建
    async newCreate() {
      // let res = await this.$api.get("/codeLookup/nCounter/RTX_YUM_RECEIPT_PREFLIGHT");
      let numberType = 11737;
      let res = await this.$api.get("/sysNumberRule/getSysNumberByType/"+numberType);
      
        this.$store.commit("setRouteId", res.data.data);

        this.$store.commit("setNewFlag", true)
        this.dialogFlag = true;
        setTimeout(() => {
          this.$router.push({ name: 'receiptPreflightDeawer' });
        }, 0)
      
    },

    async del(data) {
      try {
        let flag = false;
        let receiptpreflightkeys = [];
        data.forEach(item => {
          receiptpreflightkeys.push(item.receiptpreflightkey);
          if (item.receiptkey != null && item.receiptkey != '') {
            this.$message.error(item.receiptpreflightkey + "已存在ASN单,不能进删除操作!");
            flag = true;
            return;
          }
        });
        if (flag) {
          return;
        }
        let res = await this.$api.post(`/receiptPreflight/deleteReceiptPreflightByReceiptpreflightkeys/${receiptpreflightkeys}`, {});
        
          this.$message.success(this.$t('app.delMes'));
          let page = sessionStorage.getItem('page');
          this.getTableData(page, this.tableSearhData);
          
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    //生成ASN
    async createASN() {
      try {
        let res = await this.$api.post(`/receiptPreflight/createASNByReceiptPreflight`, {});
        
          this.$message.success(this.$t('msg.msg76'));
          let page = sessionStorage.getItem('page');
          this.getTableData(page, this.tableSearhData);
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    //选中的内容
    selectData(data) {
      this.tableSelectData = data;
    },
    async getTableData(page, data) {
      try {
        this.loading = true;
        let res = await this.$api.post(`/receiptPreflight/selectReceiptPreflightList/${page}`, data);
        
          this.tableData = res.data.data.list;
          this.page = res.data.data.pageNum;
          this.pageSize = res.data.data.pageSize;
          this.total = res.data.data.total;
        
      } catch (e) {
        this.$message.error(e);
      } finally {
        this.loading = false;
      }
    },
    //搜索的值
    searhData(data, up) {
      if (!up) {
        this.tableSearhData = data;
      } else {
        this.tableSearhData = data;
        this.getTableData(1, this.tableSearhData);
      }
    },
    //设置弹框
    setdialog(data) {
      this.$store.commit("setRouteData", data)
      this.dialogFlag = true;
      if (data.receiptkey != null) {
        this.saveStatus = false;
      }
      setTimeout(() => {
        this.$router.push({ name: 'receiptPreflightDeawer' });
      }, 0)
    },
  },
  mounted() {
    // 
    this.$store.commit("setSkip", {});
    this.$store.commit("setNewFlag", false);
    this.$store.commit("setRouteData", "");
    this.getTableData(1, {});
  },
  watch: {
    tableSelectData(data) {
      if (this.permissionStatus) {
        if (data.length > 0) {
          this.deleteStatus = false;
        } else {
          this.deleteStatus = true;
        }
      }
    },
    $route(data) {
      if (data.name === "receiptPreflight") {
        this.dialogFlag = false;
      }
    },
    dialogFlag(data) {
      if (data === false) {
        this.$store.commit("setNewFlag", false);
        this.getTableData(1, {});
        this.$router.push({ name: "receiptPreflight" });
        this.$store.commit("setSkip", {});
        this.$store.commit("setRouteData", {});
      }
    }
  },
  components: {
    "v-tableTitle": tableTitle,
    "v-table": table,
  }
};
</script>

<style scoped>
#ReceiptPreflight {
  width: 100%;
  height: 99%;
  padding-top: 6px;
}
</style>
